<?php
require_once 'base.php';
checkAuth();

$op = Utility::getParam('op', 'manage');
switch($op) {
	case 'add':
		$op();
		break;
	case 'doAdd':
		$op();
		break;
	case 'modify':
		$op();
		break;
	case 'doModify':
		$op();
		break;
	case 'delete':
		$op();
		break;
	case 'doDelete':
		$op();
		break;
	case 'manage':
		$op();
		break;
	case 'resetPassword':
		$op();
		break;
	case 'doResetPassword':
		$op();
		break;
	default:
		echoAjax(Constants::$ERR_MISSING_PARAMS);
		break;
}

function add() {
	
	$conf = $GLOBALS['conf'];
	include $GLOBALS['ROOT_PATH'] . '/views/admin/user_add.phtml';
}

function doAdd() {
	
	$username = Utility::getParam('username', '');
    $password = Utility::getParam('password', '');
    $remark = Utility::getParam('remark', '');
    $level = (int)Utility::getParam('level', 1);
    
    if (empty($username) || empty($password) || empty($level)){
    	echoAjax(Constants::$ERR_MISSING_PARAMS);
    	return;
    }
    $usernameLen = strlen($username);
    $passwordLen = strlen($password);
    if ($usernameLen < 4 || $usernameLen > 20 || $passwordLen != 32 || $level <= 0) {
    	echoAjax(Constants::$ERR_INVALID_PARAMS);
    	return;
    }
    if (ObjectFinder::get('User')->getUserInfoByUserName($username)) {
    	echoAjax(Constants::$ERR_ADD_FAILED_FOR_USER_EXIST);
    	return ;
    }
    
    $res = (int)ObjectFinder::get('User')->addUser(array(
    	'username'	=>	$username,
    	'password'	=>	$password,
    	'level'		=>	$level,
    	'remark'	=>	$remark
    ));
    echoAjax($res ? Constants::$ERR_SUCCESS : Constants::$ERR_FAILED);
}

function modify() {
	
	$conf = $GLOBALS['conf'];
	$id = max(0, (int)Utility::getParam('id', 0));
    if (empty($id)){
    	echoAjax(Constants::$ERR_INVALID_PARAMS);
    	return ;
    }
    $userInfo = ObjectFinder::get('User')->getUserInfoById($id);
    
    include $GLOBALS['ROOT_PATH'] . '/views/admin/user_modify.phtml';
}

function doModify() {
	
	$id = (int)Utility::getParam('id', 0);
    $level = (int)Utility::getParam('level', 1);
    $remark = Utility::getParam('remark', '');
    if (empty($id) || empty($level)){
    	echoAjax(Constants::$ERR_MISSING_PARAMS);
    	return;
    }
    
    ObjectFinder::get('User')->modifyUserInfo(array('level'=>$level, 'remark'=>$remark), array('id'=>$id));
    echoAjax(Constants::$ERR_SUCCESS);
}

function delete() {}

function doDelete() {
	$id = (int)Utility::getParam('id', 0);
    if (empty($id)) {
    	echoAjax(Constants::$ERR_MISSING_PARAMS);
    	return;	
    }
    $res = ObjectFinder::get('User')->deleteUserById($id);
    echoAjax($res ? Constants::$ERR_SUCCESS : Constants::$ERR_FAILED);
}

function manage() {
	$conf = $GLOBALS['conf'];
	$userInfo = $GLOBALS['userInfo'];
	$start = (int)Utility::getParam('start', 0);
    $page = (int)Utility::getParam('page', 1);
    $page = max(1, $page);
    $count = $page * $conf['pageSize'];
    $userList = ObjectFinder::get('User')->getUserList($start, $count);
    
    include $GLOBALS['ROOT_PATH'] . '/views/admin/user_manage.phtml';
}

function resetPassword() {
	
	$id = max(0, (int)Utility::getParam('id', 0));
	if (empty($id)) {
		echoAjax(Constants::$ERR_MISSING_PARAMS);
		return ;
	}
	
	$userInfo = ObjectFinder::get('User')->getUserInfoById($id);
	include $GLOBALS['ROOT_PATH'] . '/views/admin/user_resetPassword.phtml';
}

function doResetPassword() {
	
	$username = Utility::getParam('username', '');
    $password = Utility::getParam('password', '');
    if (empty($username) || empty($password)){
    	echoAjax(Constants::$ERR_MISSING_PARAMS);
    	return;
    }
    
    $usernameLen = strlen($username);
    $passwordLen = strlen($password);
    if ($usernameLen < 4 || $usernameLen > 20 || $passwordLen != 32) {
    	echoAjax(Constants::$ERR_INVALID_PARAMS);
    	return;
    }
    
    $res = ObjectFinder::get('User')->modifyPassword($username, $password);
    echoAjax($res ? Constants::$ERR_SUCCESS : Constants::$ERR_FAILED);
}
?>
